package com.zx.pay.wechat.controller;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wechat/pay/refund")
public class RefundController {

    @Autowired
    private Config config;

    @Value("${wechat.pay.merchant-id}")
    private String merchantId;

    /**
     * 申请退款
     */
    @PostMapping("/create")
    public Refund createRefund(
            @RequestParam String outTradeNo,
            @RequestParam String outRefundNo,
            @RequestParam long amount,
            @RequestParam long refund) {

        RefundService service = new RefundService.Builder().config(config).build();

        CreateRequest request = new CreateRequest();
        request.setOutTradeNo(outTradeNo);
        request.setOutRefundNo(outRefundNo);

        AmountReq amountInfo = new AmountReq();
        amountInfo.setTotal(amount);
        amountInfo.setRefund(refund);
        request.setAmount(amountInfo);

        return service.create(request);
    }

    /**
     * 查询单笔退款（通过商户退款单号）
     */
    @GetMapping("/query/{outRefundNo}")
    public Refund queryRefund(@PathVariable String outRefundNo) {
        RefundService service = new RefundService.Builder().config(config).build();

        QueryByOutRefundNoRequest request = new QueryByOutRefundNoRequest();
        request.setOutRefundNo(outRefundNo);

        return service.queryByOutRefundNo(request);
    }
}